﻿using System;
using nRoute.ViewModels;
using nRoute.Components;
using nRoute.Components.Composition;
using nRoute.Navigation.Mapping;
using nRoute.Samples.OfficerXcel.Views;
using nRoute.Samples.OfficerXcel.ViewServices;
using nRoute.Samples.OfficerXcel.Services;

namespace nRoute.Samples.OfficerXcel.ViewModels
{
    [DefineNavigationContent("Dialogs/ShareDialog", typeof(ShareDialog))]
    [MapViewModel(typeof(ShareDialog))]
    public class ShareDialogViewModel 
        : NavigationViewModelBase
    {
        private string _userName;
        private string _initials;
        private readonly IApplicationSettingsService _applicationSettingsService;
        private readonly Lazy<ITimeoutNotificationViewService> _notificationViewService;

        [ResolveConstructor]
        public ShareDialogViewModel(IApplicationSettingsService applicationSettingsService,
            Lazy<ITimeoutNotificationViewService> notificationViewService)
        {
            if (applicationSettingsService == null) throw new ArgumentNullException("applicationSettingsService");
            if (notificationViewService == null) throw new ArgumentNullException("notificationViewService");

            _applicationSettingsService = applicationSettingsService;
            _notificationViewService = notificationViewService;

            SetupCommands();
            UserName = _applicationSettingsService.AuthorInfo.Name;
            Initials = _applicationSettingsService.AuthorInfo.Initials;
        }

#region Properties

        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                if (!string.Equals(_userName, value, StringComparison.InvariantCulture))
                {
                    _userName = value;
                    SaveCommand.IsActive = true;
                    NotifyPropertyChanged(() => UserName);
                }
            }
        }

        public string Initials
        {
            get
            {
                return _initials;
            }
            set
            {
                if (!string.Equals(_initials, value, StringComparison.InvariantCulture))
                {
                    _initials = value;
                    SaveCommand.IsActive = true;
                    NotifyPropertyChanged(() => Initials);
                }
            }
        }

        public ActionCommand SaveCommand { get; private set; }
        
#endregion

#region Helpers

        private void SetupCommands()
        {
            SaveCommand = new ActionCommand(() =>
            {
                _applicationSettingsService.AuthorInfo.Name = UserName;
                _applicationSettingsService.AuthorInfo.Initials = Initials;
                _applicationSettingsService.SaveSettings();

                SaveCommand.IsActive = false;
                _notificationViewService.Value.ShowNotification("Your shared documents identifiers have been saved", 
                    TimeSpan.FromSeconds(5));
            }, false);
        }

#endregion

    }
}
